06. While Loop 2
While Loop 2
Start Quiz:
# Now, we want to ask ourselves the question: How many occurrences of
# the number 9 appear in our randomly made list?
#
# For example, if we have a list: [2,8,9,9,4,5,9], we want to figure out
# how to loop through the list and count the number of occurrences of the
# number 9. In the example list above, the number 9 occurs three times.
import random
# 1. Create random list of integers using while loop
random_list = []
list_length = 20
while len(random_list) < list_length:
random_list.append(random.randint(0,10))
# Write code here to loop through the list and count all occurrences
# of the number 9. Note that `if` statements and `while` loops will help you solve
# this problem.
# Test: If the `while` loop we wrote works, we should manually count
# how many times the number 9 is present in the list.
print random_list
print count
Solution:
Breaking Down the Problem
We first need to understand what are the inputs and what are the outputs (or results) that we want to obtain.
The inputs are:
- A list of 20 randomly generated integers
The output is:
- A number representing the count of the number of times the number 9 is in the list
What To Do
Therefore we want to loop through the list and count how many times the number 9 appears. If the current element is the number 9, we can increase the count by 1 and move on to the next element in the list.
Let's see if we can write an outline of what to do if we were to do this manually on pen and paper:
- Loop through each element in the list
- If the element is 9, we increase our count by 1
- Are we at the end of the list yet?
- If not, we loop back up and go through steps 1 to 3 again while we are still going through the list
Translation
Let's step through these steps and translate these steps into computer code.
1. Loop through each element in the list
It seems like we first need to set up the loop structure to loop through each element in the list. In order to do this, we should setup some variables to hold the current index of the list and the current count:
index = 0
count = 0
We can now set up our loop structure:
while index < len(random_list):
# Put other code here
index = index + 1
Please keep in mind how we are already adding index = index + 1 to the loop. This code is crucial to guarantee that the computer not step into an infinite loop. For most loops, we want to always clearly define a stopping point. In this case, the stopping point is when the number index is greater than the length of our list.
2. If the element is 9, we increase our count by 1
This is a classic example of an if statement. We can access the current element in the list by using random_list[index] and then use an if statement to check whether the element is 9. We can then increment the count by 1 if the element equals 9.
if random_list[index] == 9:
count = count + 1
3 - 4: Let's loop back up to our steps if we have not reached the end of the list yet.
Our loop construct already takes care of this criteria because at the top of our loop, we are always checking whether our index number is still less than the length of the random list: index < len(random_list)
We use the logic: if the index number is less than the length of our list, then we can safely say that whenever we access the list with index, we will never create an error and will be able access elements in the list with the number index.
Answer Code
index = 0
count = 0
while index < len(random_list):
if random_list[index] == 9:
count = count + 1
index = index + 1
Full Answer Code
import random
random_list = []
list_length = 20
while len(random_list) < list_length:
random_list.append(random.randint(0,10))
index = 0
count = 0
while index < len(random_list):
if random_list[index] == 9:
count = count + 1
index = index + 1